UNIFORM

Overview

The UNIFORM function computes various properties of the continuous uniform distribution, also known as the rectangular distribution. This distribution models scenarios where all outcomes within a specified interval are equally likely, making it fundamental for simulations, random number generation, and probability calculations.

The continuous uniform distribution is defined over the interval [a, b] where a is the lower bound and b is the upper bound. In this implementation, which uses SciPy’s stats module, the distribution is parameterized by loc (the lower bound a) and scale (the width of the interval b - a). The distribution therefore spans the interval [\text{loc}, \text{loc} + \text{scale}].

The probability density function (PDF) for the uniform distribution is constant across the support:

f(x) = \begin{cases} \frac{1}{\text{scale}} & \text{for } \text{loc} \leq x \leq \text{loc} + \text{scale} \\ 0 & \text{otherwise} \end{cases}

The cumulative distribution function (CDF) is:

F(x) = \frac{x - \text{loc}}{\text{scale}} \quad \text{for } \text{loc} \leq x \leq \text{loc} + \text{scale}

Key statistical properties of the uniform distribution include:

  • Mean: \mu = \text{loc} + \frac{\text{scale}}{2}
  • Variance: \sigma^2 = \frac{\text{scale}^2}{12}
  • Standard Deviation: \sigma = \frac{\text{scale}}{\sqrt{12}}

The function supports multiple computation methods: PDF (probability density), CDF (cumulative probability), ICDF (inverse CDF / quantile function), SF (survival function), ISF (inverse survival function), as well as direct calculation of mean, median, variance, and standard deviation. For more details on the underlying implementation, see the SciPy uniform distribution documentation and the SciPy GitHub repository.

This example function is provided as-is without any representation of accuracy.

Excel Usage

=UNIFORM(value, loc, scale, uniform_method)
  • value (float, optional, default: null): Input value x for pdf/cdf/sf, or probability q for icdf/isf. Not required for mean/median/var/std.
  • loc (float, optional, default: 0): Location parameter (lower bound of the distribution).
  • scale (float, optional, default: 1): Scale parameter (width of the distribution). Must be greater than 0.
  • uniform_method (str, optional, default: “pdf”): Distribution method to compute.

Returns (float): Result of the requested method, or str error message if input is invalid.

Examples

Example 1: PDF at middle of distribution

Inputs:

value loc scale uniform_method
0.5 0 1 pdf

Excel formula:

=UNIFORM(0.5, 0, 1, "pdf")

Expected output:

1

Example 2: CDF at middle of distribution

Inputs:

value loc scale uniform_method
0.5 0 1 cdf

Excel formula:

=UNIFORM(0.5, 0, 1, "cdf")

Expected output:

0.5

Example 3: Inverse CDF at probability 0.5

Inputs:

value loc scale uniform_method
0.5 0 1 icdf

Excel formula:

=UNIFORM(0.5, 0, 1, "icdf")

Expected output:

0.5

Example 4: Mean of standard uniform distribution

Inputs:

loc scale uniform_method
0 1 mean

Excel formula:

=UNIFORM(0, 1, "mean")

Expected output:

0.5

Python Code

from scipy.stats import uniform as scipy_uniform
import math

def uniform(value=None, loc=0, scale=1, uniform_method='pdf'):
    """
    Uniform distribution function supporting multiple methods.

    See: https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.uniform.html

    This example function is provided as-is without any representation of accuracy.

    Args:
        value (float, optional): Input value x for pdf/cdf/sf, or probability q for icdf/isf. Not required for mean/median/var/std. Default is None.
        loc (float, optional): Location parameter (lower bound of the distribution). Default is 0.
        scale (float, optional): Scale parameter (width of the distribution). Must be greater than 0. Default is 1.
        uniform_method (str, optional): Distribution method to compute. Valid options: PDF, CDF, ICDF, SF, ISF, Mean, Median, Variance, Std Dev. Default is 'pdf'.

    Returns:
        float: Result of the requested method, or str error message if input is invalid.
    """
    valid_methods = {'pdf', 'cdf', 'icdf', 'sf', 'isf', 'mean', 'median', 'var', 'std'}

    if not isinstance(uniform_method, str):
        return "Invalid input: uniform_method must be a string."

    method = uniform_method.lower()
    if method not in valid_methods:
        return f"Invalid method: {uniform_method}. Must be one of {', '.join(sorted(valid_methods))}."

    try:
        loc = float(loc)
        scale = float(scale)
    except (ValueError, TypeError):
        return "Invalid input: loc and scale must be numbers."

    if scale <= 0:
        return "Invalid input: scale must be > 0."

    dist = scipy_uniform(loc=loc, scale=scale)

    # Methods that require value
    if method in ['pdf', 'cdf', 'icdf', 'sf', 'isf']:
        if value is None:
            return f"Invalid input: missing required argument 'value' for method '{method}'."
        try:
            value = float(value)
        except (ValueError, TypeError):
            return "Invalid input: value must be a number."

        if method == 'pdf':
            result = dist.pdf(value)
        elif method == 'cdf':
            result = dist.cdf(value)
        elif method == 'sf':
            result = dist.sf(value)
        elif method == 'icdf':
            if not (0 <= value <= 1):
                return "Invalid input: value (probability) must be between 0 and 1 for icdf."
            result = dist.ppf(value)
        elif method == 'isf':
            if not (0 <= value <= 1):
                return "Invalid input: value (probability) must be between 0 and 1 for isf."
            result = dist.isf(value)

    # Methods that do not require value
    else:
        if method == 'mean':
            result = dist.mean()
        elif method == 'median':
            result = dist.median()
        elif method == 'var':
            result = dist.var()
        elif method == 'std':
            result = dist.std()

    if isinstance(result, float):
        if math.isnan(result):
            return "Result is NaN (not a number)"
        if math.isinf(result):
            return "inf" if result > 0 else "-inf"

    return float(result)

Online Calculator